package com.lhq.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhq.business.domain.Tag;
import com.lhq.business.service.ITagService;
import com.lhq.common.annotation.Anonymous;
import com.lhq.common.annotation.Log;
import com.lhq.common.core.controller.BaseController;
import com.lhq.common.core.domain.AjaxResult;
import com.lhq.common.enums.BusinessType;
import com.lhq.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 标签分类Controller
 *
 * @author lhq
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/business/tag")
public class TagController extends BaseController
{
    @Autowired
    private ITagService tagService;

    /**
     * 查询标签分类列表
     */
    // @PreAuthorize("@ss.hasPermi('business:tag:list')")
    @Anonymous
    @GetMapping("/list")
    public AjaxResult list(Tag tag,
                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<Tag> page =new Page<>(pageNum,pageSize);
        return AjaxResult.successPage(tagService.selectList(page,tag));
    }

    /**
     *
     * 查询标签分类树
     *
     * @author lhq
     * @date 2024/5/7 11:31
     * @version 1.0
     */
    @Anonymous
    @GetMapping("/tree")
    public AjaxResult tagTree()
    {
        List<Tag> list = tagService.tagTree();
        return AjaxResult.successPage(list);
    }

    /**
     * 导出标签分类列表
     */
    @PreAuthorize("@ss.hasPermi('business:tag:export')")
    @Log(title = "标签分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tag tag,
                       @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<Tag> page =new Page<>(pageNum,pageSize);
        List<Tag> list = tagService.selectList(page,tag).getRecords();
        ExcelUtil<Tag> util = new ExcelUtil<Tag>(Tag.class);
        util.exportExcel(response, list, "标签分类数据");
    }

    /**
     * 获取标签分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:tag:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tagService.selectById(id));
    }

    /**
     * 新增标签分类
     */
    @PreAuthorize("@ss.hasPermi('business:tag:add')")
    @Log(title = "标签分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Tag tag)
    {
        return toAjax(tagService.insertTag(tag));
    }

    /**
     * 修改标签分类
     */
    @PreAuthorize("@ss.hasPermi('business:tag:edit')")
    @Log(title = "标签分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Tag tag)
    {
        return toAjax(tagService.updateTag(tag));
    }

    /**
     * 删除标签分类
     */
    @PreAuthorize("@ss.hasPermi('business:tag:remove')")
    @Log(title = "标签分类", businessType = BusinessType.DELETE)
	@GetMapping("/del/{id}")
    public AjaxResult remove(@PathVariable("id") String  id)
    {
        return toAjax(tagService.deleteById(id));
    }
}
